<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" v-on:change="changeKeyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getShop">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="60">
			</el-table-column>
			<el-table-column prop="" label="序号" type="index" width="55">
			</el-table-column>
			<el-table-column prop="name" label="店铺名称" width="200" sortable>
			</el-table-column>
      <el-table-column prop="logo" label="店铺logo" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.logo" height="40" width="40"/>
        </template>
      </el-table-column>
      <el-table-column prop="tel" label="联系方式" width="110" sortable>
      </el-table-column>
			<el-table-column prop="registerTime" label="注册时间" width="110" sortable>
			</el-table-column>
			<el-table-column prop="address" label="地址" width="300" sortable>
			</el-table-column>
      <el-table-column prop="admin.username" label="管理员" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state === 1" style="color: red;">待审核</span>
          <span v-if="scope.row.state === 2" style="color: #50bfff;">待激活</span>
          <span v-if="scope.row.state === 3" style="color: green;">激活成功</span>
          <span v-if="scope.row.state === 4" style="color: #E6A23C;">驳回</span>
        </template>
      </el-table-column>
			<el-table-column label="操作" width="300">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="primary" v-if="scope.row.state == 1 || scope.row.state == 4" size="small" @click="manualAudit(scope.$index, scope.row)">手动审核</el-button>
          <el-button type="primary" v-if="scope.row.state == 2" size="small" @click="sendActivate(scope.$index, scope.row)">发送激活</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination 
			layout="total,prev, pager, next"
			@current-change="handleCurrentChange" 
			:page-size="query.pageSize" 
			:total="pageInfo.total" 
			:current-page="query.currentPage"
			style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑/新增界面-->
		<el-dialog style="text-align: center;" width="600px" title="编辑商家信息" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <h3>店铺入驻</h3>
        <el-form-item prop="name" label="名称">
          <el-input type="text" v-model="saveForm.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
        </el-form-item>
        <el-form-item prop="tel" label="座机">
          <el-input type="text" v-model="saveForm.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
        </el-form-item>
        <el-form-item prop="address" label="店铺地址">
          <el-input type="text" v-model="saveForm.address" auto-complete="off" placeholder="请输入地址！"></el-input>
          <!--        <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>-->
        </el-form-item>
        <h3 class="title">管理员信息设置</h3>
        <el-form-item prop="admin.username" label="账号">
          <el-input type="text" v-model="saveForm.admin.username" auto-complete="off" placeholder="请输入账号！"></el-input>
        </el-form-item>
        <el-form-item prop="admin.phone" label="手机号码">
          <el-input type="text" v-model="saveForm.admin.phone" auto-complete="off" placeholder="请输入电话！"></el-input>
        </el-form-item>
        <el-form-item prop="admin.email" label="电子邮件">
          <el-input type="text" v-model="saveForm.admin.email" auto-complete="off" placeholder="请输入邮件！"></el-input>
        </el-form-item>
        <el-form-item prop="admin.password" label="密码">
          <el-input type="password" v-model="saveForm.admin.password" auto-complete="off" placeholder="请输入密码！"></el-input>
        </el-form-item>
        <el-form-item prop="admin.confirmPassword" label="确认密码">
          <el-input type="password" v-model="saveForm.admin.confirmPassword" auto-complete="off" placeholder="请输入确认密码！"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--人工审核界面-->
    <el-dialog width="600px" title="人工审核信息" :visible.sync="checkFormVisible" :close-on-click-modal="false">
      <el-form :model="checkForm" label-width="80px" ref="saveForm">
        <el-form-item label="店铺名称">
          <el-input v-model="checkForm.shop.name" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="checkForm.shop.tel" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="checkForm.shop.address" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="Logo">
          <el-input v-model="checkForm.shop.logo" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="初始状态">
          <el-radio-group v-model="checkForm.shop.state" :disabled="true">
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="checkCancel">取消</el-button>
        <el-button type="warning" :disabled="rejectDisabled" @click.native="rejectSubmit" :loading="editLoading">驳回</el-button>
        <el-button type="success" @click.native="passSubmit" :loading="editLoading">通过</el-button>
      </div>
    </el-dialog>
    
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					username: ''
				},
				listLoading: false,
				sels: [],//列表选中列

				saveFormVisible: false,//编辑界面是否显示
				editLoading: false,
        rejectDisabled: false,// 驳回按钮是否可用
				saveFormRules: {
					username: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
        checkFormVisible: false,// 审核界面
				//编辑界面数据
        checkForm: {
          shop: {
            id: null,
            name: '',
            tel: '',
            registerTime: '',
            address: '',
            logo: '',
            state: null,
            admin:{
              id: null,
              username: '',
              email: '',
            },
          },
          type: '',
				},
        //编辑界面数据
        saveForm: {
          id: null,
          name: '',
          tel: '',
          registerTime: '',
          address: '',
          logo: '',
          state: null,
          admin:{
            username:"",
            phone:"",
            email:"",
            password:"",
            confirmPassword:""
          },
        },
				query: {
					currentPage: 1,	// 当前页，默认第一页
					pageSize : 10,// 每页展示数据，默认10条
					keyword: ""// 关键字-查询条件
				},
				pageInfo: {
					total: 0,// 总条数
					rows:[] // 当前页数据
				},
        url: "require(../../assets/1.jpg)",
			}
		},
		methods: {
      // 重新发送激活邮箱
      sendActivate(index,row) {
        console.log(row)
        this.$http.get("/reactivation",row)
          .then(result => {
            result = result.data;
            this.$message({message: result.message, type: 'success'})
          })
          .catch(result => {
            this.$message({message: "网络错误："+result,type: 'error'});
          })
      },
      // 审核框的取消按钮
      checkCancel() {
        this.checkFormVisible = false;
        this.rejectDisabled = false;
      },
      // 审核通过
      passSubmit() {
        this.checkForm.type = "pass";
        this.$confirm('确认通过该店铺申请吗?', '提示', {type: 'warning'})
          .then(() => {
            this.checkPost();
          })
          .catch(() => {
            this.$message({message: "你取消了操作！",type: 'info'})
          })
      },
      // 审核驳回
      rejectSubmit() {
        this.checkForm.type = "reject";
        this.$confirm('确认驳回该店铺申请吗?', '提示', {type: 'warning'})
          .then(() => {
            this.checkPost();
          })
          .catch(() => {
            this.$message({message: "你取消了操作！",type: 'info'})
          })
      },
      // 审核提交
      checkPost() {
        console.log(this.checkForm)
        this.$http.post("/shop/check",this.checkForm)
          .then(result => {
            result = result.data;
            this.$message({message: result.message, type: 'success'})
            this.getShopList()
            this.checkFormVisible = false;
            this.rejectDisabled = false;
          })
          .catch(result => {
            this.$message({message: "网络错误："+result,type: 'error'});
          })
      },
      // 人工审核
      manualAudit(index, row) {
        this.checkForm.shop = Object.assign({}, row);
        this.checkFormVisible = true;
        if (this.checkForm.shop.state == 4) {
          this.rejectDisabled = true;
        }
      },
      // 高级查询输入框改变时
      changeKeyword(val) {
        this.getShopList();
      },
      // 分页信息
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getShopList();
			},
			// 搜索获取店铺列表
			getShop() {
        this.query.currentPage = 1;
        this.getShopList();
			},
			//删除
			handleDel(index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {type: 'warning'})
          .then(() => {
            this.$http.delete("/shop/"+row.id)
              .then(result => {
                result = result.data;
                if (result.success) {// true 后端返回了数据
                  this.$message({message: result.message,type: 'success'});
                  this.query.currentPage = 1;
                  this.getShopList();
                } else {// 后端处理失败
                  this.$message({message: result.message,type: 'error'});
                }
              })
              .catch(result => {
                this.$message({message: "网络错误："+result,type: 'error'});
              })
            console.log(row)
          })
          .catch(() => {
            this.$message({message: "你取消了删除",type: 'info'})
          });
			},
      //显示编辑界面
			handleEdit(index, row) {
				this.saveFormVisible = true;
        console.log("数据回显",row)
				this.saveForm = Object.assign({}, row);
        // 特殊处理manager parent
        // 如果manager parent没有值,为null的话,就给它们赋一个初识对象
        if(!this.saveForm.dept){
          this.saveForm.dept = {
            id:null,
            name: ""
          }
        }
        if (!this.saveForm.role) {
          this.saveForm.role = {
            id: null,
            name: '',
          }
        }
        console.log("数据回显22",this.saveForm)
			},
			//新增界面
			handleAdd() {
				this.saveFormVisible = true;
				this.saveForm = {
          id: null,
          name: '',
          tel: '',
          registerTime: '',
          address: '',
          logo: '',
          state: null,
          admin:{
            username:"",
            phone:"",
            email:"",
            password:"",
            confirmPassword:""
          },
				};
			},
			//编辑
			editSubmit() {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {})
              .then(() => {
                this.editLoading = true;  // 加载框
                // 调用接口
                console.log("提交数据：",this.saveForm)
                this.$http.put("/shop",this.saveForm)
                  .then(result => {
                    result = result.data;
                    if (result.success) {// true 后端返回了数据
                      this.$message({message: result.message,type: 'success'});
                    } else {// 后端处理失败
                      this.$message({message: result.message,type: 'error'});
                    }
                    this.editLoading = false;
                    this.saveFormVisible = false;
                    this.getShop();
                  })
                  .catch(result => {
                    alert("网络错误！"+result)
                  })
                console.log(this.saveForm)
              })
					}
				});
			},
      // 多选框改变时
			selsChange(sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove() {
				let ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'})
          .then(() => {
            console.log(ids)
            this.$http.patch("/shop",ids)
              .then(result => {
                result = result.data;
                if (result.success) {// true 后端返回了数据
                  this.$message({message: result.message,type: 'success'});
                } else {// 后端处理失败
                  this.$message({message: result.message,type: 'error'});
                }
                this.query.currentPage = 1;
                this.getShopList();
              })
              .catch(result => {
                alert("网络错误！"+result)
              })
          })
          .catch(() => {
            this.$message({message: "你取消了删除",type: 'info'})
          });
			},
			// 获取分页数据
			getShopList() {
				console.log(this.query);
				this.$http.post("/shop", this.query)
					.then(result => {
						console.log(result);
						result = result.data;
						if (result.success) {// true 后端返回了数据
							this.pageInfo = result.data;
						} else {// 后端处理失败
              this.$message({message: result.message,type: 'error'});
            }
					})
          .catch(result => {// 连接后端接口失败
            alert("网络错误！"+result)
          })
			}
		},
		mounted() {
			this.getShopList();
		}
	}

</script>

<style scoped>
.head_form {
  padding: 10px 20px 30px 20px;
}
</style>